package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.query.BpmnQuery;
import cn.wolfcode.car.business.service.BpmnInfoService;
import cn.wolfcode.car.business.service.CarPackageAuditService;
import cn.wolfcode.car.business.service.ServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.image.ProcessDiagramGenerator;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.Collections;
import java.util.List;

/**
 * @Author WaveHi
 * @Date 17/11/2021 上午 10:45
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@Service
public class BpmnInfoServiceImpl extends ServiceImpl<BpmnInfoMapper, BpmnInfo> implements BpmnInfoService {
//
//    @Autowired
//    private BpmnInfoMapper bpmnInfoMapper;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private CarPackageAuditService carPackageAuditService;

    @Autowired
    private ServiceItemService serviceItemService;

    @Override
    public TablePageInfo<BpmnInfo> getPartOfBpmnInfos(BpmnQuery bpmnQuery) {
        QueryWrapper<BpmnInfo> bpmnInfoQueryWrapper = new QueryWrapper<>();
        bpmnInfoQueryWrapper.ge(bpmnQuery.getBeginTime() != null,
                "deploy_time",
                bpmnQuery.getBeginTime())
                .le(bpmnQuery.getEndTime() != null,
                        "deploy_time", bpmnQuery.getEndTime());
        return new TablePageInfo<>(list(bpmnInfoQueryWrapper));
//        return new TablePageInfo(bpmnInfoMapper.selectByCondition(bpmnQuery));
    }

    @Override
    public Boolean add(BpmnInfo bpmnInfo) {
        return save(bpmnInfo);
//        int insert = bpmnInfoMapper.insert(bpmnInfo);
//        return insert > 0;
    }

    @Override
    public InputStream getResources(String deploymentId, String type) {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploymentId)
                .singleResult();
        if("xml".equalsIgnoreCase(type)) {
            return repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName());
        }
        if("png".equalsIgnoreCase(type)) {
            BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId());
            ProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
            //generateDiagram(流程模型,需要高亮的节点,需要高亮的线条,后面三个参数都表示是字体)
            InputStream inputStream = generator.generateDiagram(bpmnModel, Collections.EMPTY_LIST, Collections.EMPTY_LIST,
                    "宋体","宋体","宋体");
            return inputStream;
        }
        return null;
    }

    @Override
    public BpmnInfo queryByType(String car_package) {
        QueryWrapper<BpmnInfo> bpmnInfoQueryWrapper = new QueryWrapper<>();
        bpmnInfoQueryWrapper.eq("bpmn_type", car_package);
        return getOne(bpmnInfoQueryWrapper);
//        return bpmnInfoMapper.selectByType(car_package);
    }

    @Override
    public BpmnInfo getBpmnInfo(Long bpmnInfoId) {
        return getById(bpmnInfoId);
//        return bpmnInfoMapper.selectByPrimaryKey(bpmnInfoId);
    }

    @Override
    @Transactional
    public Boolean delete(Long id) {
        //Get Bpmn info
        BpmnInfo bpmnInfo = this.getBpmnInfo(id);
        //Get instances
        List<ProcessInstance> list = runtimeService.createProcessInstanceQuery()
                .processDefinitionKey(bpmnInfo.getActProcessKey())
                .list();
        //Set status of car package audit and service item
        for (ProcessInstance processInstance : list) {
            CarPackageAudit carPackageAudit =
                    carPackageAuditService
                            .getCarPackageAudit(Long.valueOf(processInstance.getBusinessKey()));
//            CarPackageAudit carPackageAudit = carPackageAuditService
//                    .getCarPackageAudit(Long.valueOf(processInstance.getBusinessKey()));
            carPackageAudit.setStatus(CarPackageAudit.STATUS_CANCEL);
            carPackageAuditService.updateById(carPackageAudit);
//            carPackageAuditService.update(carPackageAudit);
            ServiceItem byId = serviceItemService.getById(carPackageAudit.getServiceItemId());
            byId.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
            serviceItemService.updateById(byId);
//            serviceItemService.changeAuditStatus(carPackageAudit.getServiceItemId(), ServiceItem.AUDITSTATUS_INIT);
        }
        //Remove process definition
        boolean b = removeById(id);
//        int delete = bpmnInfoMapper.deleteByPrimaryKey(id);
        repositoryService.deleteDeployment(bpmnInfo.getDeploymentId(), true);
        return b;
//        return delete > 0;
    }
}
